setwd("~/Library/CloudStorage/OneDrive-VrijeUniversiteitAmsterdam/Documents/replication_package/")
source("code/analyze_data/global.R")

list_of_robust_checks <- list("00", "10", "11")


df_winners_and_losers <- map_df(list_of_robust_checks, 
                                characterizeWinnersAndLosers,
                                mechanism = "srda")

df_plot <- df_winners_and_losers %>% 
    pivot_longer(cols = c("count", "g1", "g2", "g3"), 
                          names_to = "var") %>% 
    select(-mechanism) %>% 
    rbind(., df_core_size_summary) %>% 
    mutate(robust = case_when(
                robust == "11" ~ "Baseline",
                robust == "10" ~ "Approach 1",
                robust == "00" ~ "Approach 2"),
           var = case_when(
                var == "count" & winner == 2 ~ "Assigned to a contract",
                var == "funded" ~ "Assigned to a state-funded contract",
                var == "unfunded" ~ "Assigned to a self-funded contract",
                var == "full_time_unfunded" ~ "Assigned to a self-funded, full-time contract",
                var == "count" ~ "Total",
                var == "g1" & winner == 1 ~ "Newly assigned",
                var == "g1" & winner == 0 ~ "Newly unassigned",
                var == "g2" ~ "New program",
                var == "g3" & winner == 1 ~ "Same program, preferred financial terms",
                var == "g3" & winner == 0 ~ "Same program, less preferred financial terms"),
           winner = case_when(
               winner == 1 ~ "B. Winners",
               winner == 0 ~ "C. Losers",
               winner == 2 ~ "A. Change in the number of assigned applicants (Preference Flip Algorithm vs. SR-DA)"
           )) %>% 
    mutate(winner = factor(winner, ordered = T, levels = c("A. Change in the number of assigned applicants (Preference Flip Algorithm vs. SR-DA)",
                                                           "B. Winners", 
                                                           "C. Losers")),
           var = factor(var, ordered = T, 
                        levels = c("Assigned to a contract",
                                   "Assigned to a state-funded contract",
                                   "Assigned to a self-funded contract",
                                   "Assigned to a self-funded, full-time contract",
                                   "Total", "Newly assigned",
                                   "Newly unassigned", "New program", 
                                   "Same program, preferred financial terms",
                                   "Same program, less preferred financial terms")))
           

df_plot %>% 
    ggplot(aes(x = fct_reorder(var, desc(var)), y = value, shape = robust)) +
    geom_point(position=position_dodge(width = .6), size = 3) +
    coord_flip() +
    facet_wrap(winner ~., ncol = 1, scale = "free", drop = T) +
    ylab("Number of applicants") +
    theme(legend.position = "bottom") +
    labs(shape = "") +
    xlab("") +
    ylim(c(-500, 6000))
ggsave(glue("{RESULTS_FOLDER}/FigureF1.pdf"),
       device = pdf(),
       width = 20, height = 16, units = "cm")

